//
//  DatatableFlowLayout.swift
//  swiftDatatable
//
//  Created by 李朋 on 17/3/14.
//  Copyright © 2017年 李朋. All rights reserved.
//

import UIKit

class DatatableFlowLayout: UICollectionViewFlowLayout {

    lazy var attributes = [UICollectionViewLayoutAttributes]();
    
    fileprivate let space: Int = 0
    fileprivate var itemSizeW: Int = 80
    fileprivate let itemSizeH: Int = 60
    fileprivate var collectionW: CGFloat = 0
    fileprivate var collectionH: CGFloat = 0
    fileprivate var startY: CGFloat = 0
    
    var x:Int = 0;
    
    override func prepare() {
    
        super.prepare()
//        attributes.removeAll()
        
        
        //MARK: -大优化
        guard attributes.isEmpty else {
            return
        }
        
        
        collectionH = 0
        collectionW = 0
        startY = 0
        guard let collectionView = self.collectionView else {
            DLog("有问题")
            return
        }
        
        let methodStart = Date()
        
        
        
//        let column: Int = collectionView.numberOfSections
        for i in 0..<collectionView.numberOfSections {
//            startY += startY + 60.0
            collectionW = 0.0
            for j in 0..<collectionView.numberOfItems(inSection: i) {
                let index = IndexPath(row: j, section: i)
                let attribut = UICollectionViewLayoutAttributes(forCellWith: index)
                let x = Int(collectionW) + space;
                let y = i * (itemSizeH + space) + 60;
                itemSizeW = Int(rowViewModel.sharedModel.getItemWithIndexPath(indexPath: j))
                attribut.frame = CGRect(x: x, y: y, width: itemSizeW, height: itemSizeH)
                attributes.append(attribut)
                
                if collectionW < attribut.frame.maxX {
                    collectionW = attribut.frame.maxX
                }
                
                if collectionH < attribut.frame.maxY {
                    collectionH = attribut.frame.maxY
                }
                
//                if 0 == i {
//                    let index = IndexPath(row: j, section: 0)
//                    let heardAttribut =  UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, with: index)
//                    heardAttribut.frame = CGRect(x: x, y: 0, width: itemSizeW, height: 60)
//                    heardAttribut.frame.origin.y = collectionView.contentOffset.y
//                    attributes.append(heardAttribut)
//                }
//                
//                
//                if collectionView.numberOfSections - 1 == i {
//                    let index = IndexPath(row: j, section: i)
//                    let heardAttribut =  UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, with: index)
//                    heardAttribut.frame = CGRect(x: x, y: Int(collectionH), width: itemSizeW, height: 60)
//                    attributes.append(heardAttribut)
//                }
            }
            
            
        }
        
        let methodFinish = Date()
        let executionTime = methodFinish.timeIntervalSince(methodStart)
        print("Prepare method: exec-time: \(executionTime)")
    }
    
    
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        return true
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        
        var caches = attributes.filter{ $0.frame.intersects(rect) }
        for i in 0..<6 {
            
            let hearderIndexPath = IndexPath(row: i, section: 0)
            
            if let hearderAttributs = layoutAttributesForSupplementaryView(ofKind: UICollectionElementKindSectionHeader, at: hearderIndexPath) {
                caches.append(hearderAttributs)
            }
            
            let footerIndexPath = IndexPath(row: i, section: 99)
            
            if let footerAttributs = layoutAttributesForSupplementaryView(ofKind: UICollectionElementKindSectionFooter, at: footerIndexPath) {
                caches.append(footerAttributs)
            }
            
        }
        return caches;
    }

    
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        return attributes[indexPath.item]
    }
    
    override var collectionViewContentSize: CGSize {
    
        return CGSize(width: collectionW, height: collectionH + 60)
    }
    
    override func layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        if elementKind == UICollectionElementKindSectionHeader {
            let heardAttribut =  UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, with: indexPath)
            let x = Array(0..<indexPath.row).reduce(0) { $0 + Int(rowViewModel.sharedModel.getItemWithIndexPath(indexPath: $1)) }
            heardAttribut.frame = CGRect(x: x, y: 0, width: Int(rowViewModel.sharedModel.getItemWithIndexPath(indexPath: indexPath.row)), height: 60)
            heardAttribut.frame.origin.y = collectionView!.contentOffset.y
            heardAttribut.zIndex += 1
            return heardAttribut
        }else if elementKind == UICollectionElementKindSectionFooter {
            let footerAttribut =  UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, with: indexPath)
            let x = Array(0..<indexPath.row).reduce(0) { $0 + Int(rowViewModel.sharedModel.getItemWithIndexPath(indexPath: $1)) }
            footerAttribut.frame = CGRect(x: x, y: 0, width: Int(rowViewModel.sharedModel.getItemWithIndexPath(indexPath: indexPath.row)), height: 60)
            footerAttribut.frame.origin.y = collectionView!.contentOffset.y + UIScreen.main.bounds.height - 60
            footerAttribut.zIndex += 2
            return footerAttribut
        }
        return nil
    }
  
}










